package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciDirectInsuranceDetailMapper;
import com.zatech.cgnci.project.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 直保明细表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@Service
public class CgnciDirectInsuranceDetailServiceImpl extends ServiceImpl<CgnciDirectInsuranceDetailMapper, CgnciDirectInsuranceDetailDO> implements CgnciDirectInsuranceDetailService {

    @Resource
    CgnciDirectInsuranceService directInsuranceService;
    @Resource
    CgnciDirectInsuranceRiskService directInsuranceRiskService;
    @Resource
    CgnciBusinessProjectService businessProjectService;
    @Resource
    CgnciBusinessInsuranceService businessInsuranceService;
    @Resource
    CgnciReinsuranceInfoService reinsuranceInfoService;
    @Resource
    CgnciReinsuranceRiskService reinsuranceRiskService;
    @Resource
    CgnciReinsuranceRiskInsuredService reinsuranceRiskInsuredService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciUnderwritingPolicyService underwritingPolicyService;


    @Override
    public void deleteInsuranceDetail(Long detailId, String type) {
        // 查询直保标的明细
        CgnciDirectInsuranceDetailDO directInsuranceDetailDO = this.baseMapper.selectById(detailId);
        if (VerifyUtils.isEmpty(directInsuranceDetailDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "detailId有误");
        }
        if (VerifyUtils.isEmpty(directInsuranceDetailDO.getRowKey())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, detailId + ":直保标的rowKey为空");
        }

        // 根据险种标的id查询对应的直保险种
        CgnciDirectInsuranceRiskDO insuranceRiskDO = directInsuranceRiskService.getById(directInsuranceDetailDO.getDirectId());
        if (VerifyUtils.isEmpty(insuranceRiskDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到直保险种信息");
        }
        if (VerifyUtils.isEmpty(insuranceRiskDO.getRiskSubTypeCode())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "直保险种riskSubTypeCode为空");
        }

        // 根据直保险种id查询对应的直保信息
        CgnciDirectInsuranceDO directInsuranceDO = directInsuranceService.getById(insuranceRiskDO.getDirectInsuranceId());
        if (VerifyUtils.isEmpty(directInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到直保信息");
        }
        // 处理直保信息汇总字段
        directInsuranceService.updateDirectInsuranceColumn(insuranceRiskDO, directInsuranceDO, directInsuranceDetailDO.getDetailId());
        // 删除直保险种标的明细
        this.baseMapper.deleteById(detailId);

        // 查询业务号
        Long businessId = 0L;
        switch (type) {
            case "1":
                CgnciBusinessProjectDO businessProjectDO = businessProjectService.getById(directInsuranceDO.getBusinessId());
                businessId = businessProjectDO.getProjectId();
                break;
            case "2":
                CgnciBusinessInsuranceDO businessInsuranceDO = businessInsuranceService.getById(directInsuranceDO.getBusinessId());
                businessId = businessInsuranceDO.getInsuranceId();
                break;
            default:
        }

        // 删除分人险种标的
        reinsuranceRiskInsuredService.deleteReinsuranceRiskInsured("1", businessId, insuranceRiskDO.getRiskSubTypeCode(), directInsuranceDetailDO.getRowKey());

        // 删除临分分出险种标的
        reinsuranceRiskInsuredService.deleteReinsuranceRiskInsured("3", businessId, insuranceRiskDO.getRiskSubTypeCode(), directInsuranceDetailDO.getRowKey());

        // 根据删除的直保险种标，联动删除分人/分出险种标的以及分人方案/再保人险种标的，并重新计算险种标的明细以及处理汇总字段
        // reinsuranceRiskInsuredService.deleteReinsuranceRiskInsuredByDirect(businessId, insuranceRiskDO.getRiskId(), insuranceRiskDO.getRiskSubTypeCode(), directInsuranceDetailDO.getRowKey());

    }

}
